#!/usr/bin/env python
import threading
from socket import *
from time import ctime

#HOST = '192.168.199.179'
#HOST = '127.0.0.1'
HOST = '192.168.199.227'
PORT = 8266
ADDR = (HOST, PORT) 
addr = (HOST, PORT) 
IS_RUN = 1

def asocket():
	global addr
	while True: 
		if IS_RUN == 0:
			break
		data,addr = udpServerSock.recvfrom(1024)
		#udpServerSock.sendto('[%s] %s'% (ctime(),data),addr)
		print addr,">>",data
	
if __name__ == "__main__":
	print "Start UDP server...EXIT to exit."
	udpServerSock = socket(AF_INET,SOCK_DGRAM)
	udpServerSock.bind(ADDR)

	print "Start receive thread..."
	t_asocket = threading.Thread(target=asocket,args=())
	t_asocket.setDaemon(True)
	t_asocket.start()

	while True:
		indata = raw_input('') 
		if (indata=="EXIT"):
			IS_RUN = 0
			udpServerSock.close()
			print "exited."
			exit()
		udpServerSock.sendto(indata,addr)


